package com.ruoyi.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.api.domain.MerchantEvaluation;
import com.ruoyi.api.domain.Order;
import com.ruoyi.api.dto.MerchantEvaluationDto;
import com.ruoyi.api.mapper.MerchantEvaluationMapper;
import com.ruoyi.api.mapper.MerchantMapper;
import com.ruoyi.api.mapper.OrderMapper;
import com.ruoyi.api.mapper.ProductsMapper;
import com.ruoyi.api.service.IMerchantEvaluationService;
import com.ruoyi.api.vo.MerchantEvaluationVo;
import com.ruoyi.api.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 寻觅
 * @since 2025-06-11
 */
@Service
public class MerchantEvaluationServiceImpl extends ServiceImpl<MerchantEvaluationMapper, MerchantEvaluation> implements IMerchantEvaluationService {

    @Autowired
    MerchantEvaluationMapper merchantEvaluationMapper;

    @Autowired
    ProductsMapper productsMapper;

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public List<MerchantEvaluationVo> listData(MerchantEvaluationDto merchantEvaluationDto) {
        ArrayList<MerchantEvaluationVo> merchantEvaluationVos = new ArrayList<>();
//        获取商品信息
        List<MerchantEvaluation> merchantEvaluations = merchantEvaluationMapper.selectListData(merchantEvaluationDto);
        for (MerchantEvaluation merchantEvaluation : merchantEvaluations) {

            Order order = orderMapper.selectById(merchantEvaluation.getOrderId());
            MerchantEvaluationVo merchantEvaluationVo = new MerchantEvaluationVo();
            BeanUtils.copyProperties(merchantEvaluation, merchantEvaluationVo);
//            商品信息
            merchantEvaluationVo.setName(productsName(order.getProductId(), order.getProductQuantityOne()));
            merchantEvaluationVo.setUserName(order.getUserName());
            merchantEvaluationVos.add(merchantEvaluationVo);
        }
        return merchantEvaluationVos;
    }

    public String productsName(String productIds, String productQuantity) {
        if (productIds == null || productQuantity == null) {
            return null;
        }

        String[] idArray = productIds.split(",");
        String[] quantityArray = productQuantity.split(",");

        // 检查两个数组长度是否一致
        if (idArray.length != quantityArray.length) {
            System.err.println("商品ID数量与商品数量不匹配");
            return null;
        }

        StringBuilder result = new StringBuilder();

        for (int i = 0; i < idArray.length; i++) {
            try {
                String productId = idArray[i];
                String quantityStr = quantityArray[i];

                ProductVo product = productsMapper.ById(Integer.valueOf(productId));

                if (product != null) {
                    // 拼接商品名称和数量
                    if (result.length() > 0) {
                        result.append(",");
                    }
                    result.append(product.getName()).append("*").append(quantityStr);
                } else {
                    System.err.println("商品ID不存在: " + productId);
                }
            } catch (Exception e) {
                System.err.println("处理商品信息时出错, 索引: " + i + ", 错误: " + e.getMessage());
            }
        }

        return result.toString();
    }
}
